﻿Imports System.Runtime.Serialization
Imports System.Text.RegularExpressions

Namespace WebService

    <DataContract()> _
    Public Class ParentPage
        Inherits PageItem

        Private Shared ALPHANUMERIC As Regex = New Regex("^[a-zA-Z0-9]*$")

        Private _code As String
        Private _urgent As Boolean = False
        Private _location As String

        Public Sub New()
            MyBase.New()
        End Sub

        ''' <summary>
        ''' Read-Only Property to retrieve the text code for this Page Item.
        ''' </summary>
        <DataMember()> _
        Public Property Code As String
            Get
                Return _code
            End Get
            Set(ByVal value As String)
                If value IsNot Nothing Then
                    _code = value.ToUpper
                End If
            End Set
        End Property

        ''' <summary>
        ''' Read-Only Property to retrieve the Location to which this page is requesting individual should return to.
        ''' </summary>
        <DataMember()> _
        Public Property Location As String
            Get
                Return _location
            End Get
            Set(ByVal value As String)
                _location = value
            End Set
        End Property

        ''' <summary>
        ''' Read-Only Property to indicate if this Page is urgent.
        ''' </summary>
        <DataMember()> _
        Public Property IsUrgent As Boolean
            Get
                Return _urgent
            End Get
            Set(ByVal value As Boolean)
                _urgent = value
            End Set
        End Property

        Protected Overrides Function GetPageTypeName() As String
            Return "Parent"
        End Function

        Public Overrides Function Validate() As Boolean
            Return (Not IsNothing(Code) AndAlso
                    Code.Length = 3 AndAlso
                    ALPHANUMERIC.IsMatch(Code))
        End Function

        Protected Overrides Function CopyFrom(ByVal page As PageItem) As Boolean
            Dim rv As Boolean = False

            ' Only copy over mutable properties, those that can be changed once 
            ' page has been created.
            If (TypeOf page Is ParentPage) Then
                rv = True
                Dim pp = CType(page, ParentPage)
                If (hasValue(pp.IsUrgent)) Then
                    Me.IsUrgent = pp.IsUrgent
                End If
                If (hasValue(pp.Location)) Then
                    Me.Location = pp.Location
                End If
            End If

            Return rv

        End Function

        Protected Overrides Function GetPrimaryValue() As String
            Return Me.Code
        End Function

        Public Overrides Function UserPropertiesMatch(ByVal other As PageItem) As Boolean
            Dim rv = False
            If (other IsNot Nothing AndAlso TypeOf other Is ParentPage) Then
                Dim pp = CType(other, ParentPage)
                rv = (MyBase.UserPropertiesMatch(other) AndAlso
                      Me.Code = pp.Code AndAlso
                      Me.IsUrgent = pp.IsUrgent AndAlso
                      Me.Location = pp.Location)
            End If
            Return rv
        End Function

        Overrides Function ToString() As String
            Dim rv As String = "(Code=" + Me.Code +
                               ", Type=" + Me.Type +
                               ", IsUrgent=" + Me.IsUrgent.ToString() + ")"
            Return rv
        End Function

    End Class

End Namespace
